#include "miniobject.h"

MiniObject::MiniObject(int id)
{
    db = QSqlDatabase::database();
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        if ( id == -1 )
        {
            q.exec("INSERT INTO ministranten (id) VALUES (NULL);");
            this->id = q.lastInsertId().toInt();
        }
        else
        {
            this->id = id;
            q.prepare("SELECT name, vorname, anschrift, plz, ort, telefon, telefax, email FROM ministranten WHERE id=:id");
            q.bindValue(":id", id);
            if ( q.exec() && q.first() )
            {
                name        = q.value(0).toString();
                vorname     = q.value(1).toString();
                anschrift   = q.value(2).toString();
                plz         = q.value(3).toString();
                ort         = q.value(4).toString();
                telefon     = q.value(5).toString();
                telefax     = q.value(6).toString();
                email       = q.value(7).toString();
            }
        }
        q.finish();
    }

    listWidgetItem = new QListWidgetItem( (name.isEmpty())?"#neu":QString("%1, %2").arg(name, vorname) );
    listWidgetItem->setData(Qt::UserRole, this->id);
}

bool MiniObject::deleteMini()
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        q.prepare("DELETE FROM ministranten WHERE id = :id;");
        q.bindValue(":id", id);
        if ( q.exec() )
        {
            listWidgetItem->~QListWidgetItem();
            return true;
        }
    }
    return false;
}

void MiniObject::setName(QString name)
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        q.prepare("UPDATE ministranten SET name = :name WHERE id = :id;");
        q.bindValue(":name", name);
        q.bindValue(":id", id);
        if ( q.exec() )
        {
            this->name = name;
            listWidgetItem->setText(QString("%1, %2").arg(name, vorname));
        }
    }
}

void MiniObject::setVorname(QString vorname)
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        q.prepare("UPDATE ministranten SET vorname = :vorname WHERE id = :id;");
        q.bindValue(":vorname", vorname);
        q.bindValue(":id", id);
        if ( q.exec() )
        {
            this->vorname = vorname;
            listWidgetItem->setText(QString("%1, %2").arg(name, vorname));
        }
    }
}

void MiniObject::setAnschrift(QString anschrift)
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        q.prepare("UPDATE ministranten SET anschrift = :anschrift WHERE id = :id;");
        q.bindValue(":anschrift", anschrift);
        q.bindValue(":id", id);
        if ( q.exec() )
            this->anschrift = anschrift;
    }
}

void MiniObject::setPLZ(QString plz)
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        q.prepare("UPDATE ministranten SET plz = :plz WHERE id = :id;");
        q.bindValue(":plz", plz);
        q.bindValue(":id", id);
        if ( q.exec() )
            this->plz = plz;
    }
}

void MiniObject::setOrt(QString ort)
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        q.prepare("UPDATE ministranten SET ort = :ort WHERE id = :id;");
        q.bindValue(":ort", ort);
        q.bindValue(":id", id);
        if ( q.exec() )
            this->ort = ort;
    }
}

void MiniObject::setTelefon(QString telefon)
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        q.prepare("UPDATE ministranten SET telefon = :telefon WHERE id = :id;");
        q.bindValue(":telefon", telefon);
        q.bindValue(":id", id);
        if ( q.exec() )
            this->telefon = telefon;
    }
}

void MiniObject::setTelefax(QString telefax)
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        q.prepare("UPDATE ministranten SET telefax = :telefax WHERE id = :id;");
        q.bindValue(":telefax", telefax);
        q.bindValue(":id", id);
        if ( q.exec() )
            this->telefax = telefax;
    }
}

void MiniObject::setEmail(QString email)
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q(db);
        q.prepare("UPDATE ministranten SET email = :email WHERE id = :id;");
        q.bindValue(":email", email);
        q.bindValue(":id", id);
        if ( q.exec() )
            this->email = email;
    }
}

bool MiniObject::operator <(MiniObject *a)
{
    return ( id < a->getId() )?true:false;
}

int MiniObject::getId() const
{
    return id;
}

QString MiniObject::getName() const
{
    return name;
}

QString MiniObject::getVorname() const
{
    return vorname;
}

QString MiniObject::getFullName() const
{
    return QString("%1, %2").arg(name, vorname);
}

QString MiniObject::getAnschrift() const
{
    return anschrift;
}

QString MiniObject::getPLZ() const
{
    return plz;
}

QString MiniObject::getOrt() const
{
    return ort;
}

QString MiniObject::getTelefon() const
{
    return telefon;
}

QString MiniObject::getTelefax() const
{
    return telefax;
}

QString MiniObject::getEmail() const
{
    return email;
}

QListWidgetItem *MiniObject::getListWidgetItem()
{
    return listWidgetItem;
}

void MiniObject::startTransaction()
{
    QSqlQuery("BEGIN TRANSACTION;", db);
}

void MiniObject::commitTransaction()
{
    QSqlQuery("COMMIT;", db);
}
